﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using IndoorPosition.Server.Core.Entities;
using IndoorPosition.Server.Core.Utils;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IndoorPosition.Server.Application
{
    /// <summary>
    /// 楼层相关逻辑代码
    /// </summary>
    public class FloorService : IFloorService, ITransient
    {
        IRepository<Floor> _floorRepository;
        IRepository<Building> _buildingRepository;

        public FloorService(IRepository<Floor> floorRepository, IRepository<Building> buildingRepository)
        {
            _floorRepository = floorRepository;
            _buildingRepository = buildingRepository;
        }

        /// <summary>
        /// 添加楼层
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(FloorInputDto input)
        {
            var existBuilding = await _buildingRepository.Where(b => b.Id == input.BuildingId).AnyAsync();
            if (!existBuilding)
            {
                throw Oops.Oh("建筑物ID不正确");
            }
            var floor = input.Adapt<Floor>();
            if (input.Model != null)
            {
                floor.Model = await FileUtil.SaveFileAsync(input.Model, "floor");
            }
            if (input.Route != null)
            {
                floor.Route = await FileUtil.SaveFileAsync(input.Route, "floor");
            }
            var result = await _floorRepository.InsertNowAsync(floor);
            return result.Entity.Id;
        }

        /// <summary>
        /// 删除楼层
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _floorRepository.DeleteNowAsync(id);
        }

        /// <summary>
        /// 根据ID获取楼层信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FloorDto> GetAsync(int id)
        {
            var floor = await _floorRepository.Where(f => f.Id == id).Include("Building").Include("Stations").FirstOrDefaultAsync();
            return floor.Adapt<FloorDto>();
        }

        /// <summary>
        /// 分页获建筑信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedList<FloorDto>> ListAsync(PageInputDto input)
        {
            var query = _floorRepository.AsQueryable();
            if (!string.IsNullOrEmpty(input.Keyword))
            {
                query = query.Where(p => p.Name.Contains(input.Keyword));
            }
            if (input.Conditions != null && input.Conditions.Count > 0)
            {
                query = query.Where(LambdaExpressionBuilder.BuildLambda<Floor>(input.Conditions));
            }
            var floorPageList = await query.Include("Building").Include("Stations").ToPagedListAsync(input.Page, input.Size);
            return floorPageList.Adapt<PagedList<FloorDto>>();
        }

        /// <summary>
        /// 下载模型文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task<IActionResult> DownloadModelAsync(string fileName)
        {
            return new FileStreamResult(FileUtil.GetStream(fileName, "floor"), "model/gltf-binary") { FileDownloadName = fileName };
        }

        /// <summary>
        /// 下载路径文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task<IActionResult> DownloadRouteAsync(string fileName)
        {
            return new FileStreamResult(FileUtil.GetStream(fileName, "floor"), "application/json") { FileDownloadName = fileName };
        }
    }
}
